//
//  CXTabBarItem.swift
//  Project03-Weibo-swift
//
//  Created by w on 16/1/25.
//  Copyright © 2016年 w. All rights reserved.
//

import UIKit

/// 目的：只要修改了tabBarItem的badgeValue属性，其使用的样式就是自定义的样式
class CXTabBarItem: UITabBarItem {
    
    var vc: CXMainController?
    
    //通过继承来重写某个指定类的指定成员
    override var badgeValue: String? {
        
        didSet {
            //KVC取私有属性
            var count:UInt32 = 0
            let ivarlist = class_copyIvarList(UITabBarItem.self, &count)
            for i in 0..<count {
                let ivar = ivarlist[Int(i)]
                let cname = ivar_getName(ivar)
                let ctype = ivar_getTypeEncoding(ivar)
                let nameStr = NSString(CString: cname, encoding: NSUTF8StringEncoding)
                let typeStr = NSString(CString: ctype, encoding: NSUTF8StringEncoding)
                print("\(nameStr) \(typeStr)")
            }
            //let target = valueForKey("_target")! as! UITabBarController
            print(vc)
            let target = vc!
            
//           // print(target)
//           //    .... target类型是 @。。。。
//            let tabBar = target.tabBar
//            //遍历查找tabBar中的各个子控件的属性
//            for tabBarView in tabBar.subviews {
//                if tabBarView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
//                    
//                    for tabButtonChild in tabBarView.subviews {
//                        if tabButtonChild.isKindOfClass(NSClassFromString("_UIBadgeView")!) {
//                            
//                            for tabBadgetChild in tabButtonChild.subviews {
//                                if tabBadgetChild.isKindOfClass(NSClassFromString("_UIBadgeBackground")!) {
//                                    //Got it！
//                                    //KVC黑魔法开动！
//                                    tabBadgetChild.setValue(UIImage(named: "main_badge"), forKey: "_image")
//                                }
//                            }
//                        }
//                    }
//                }
            
                        let tabBar = target.tabBar
                        //遍历查找tabBar中的各个子控件的属性
                        for tabBarView in tabBar.subviews {
                            if tabBarView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
            
                                for tabButtonChild in tabBarView.subviews {
                                    if tabButtonChild.isKindOfClass(NSClassFromString("_UIBadgeView")!) {
            
                                        for tabBadgetChild in tabButtonChild.subviews {
                                            if tabBadgetChild.isKindOfClass(NSClassFromString("_UIBadgeBackground")!) {
                                                //Got it！
                                                //KVC黑魔法开动！
                                                tabBadgetChild.setValue(UIImage(named: "main_badge"), forKey: "_image")
                                            }
                                        }
                                    }
                                }
                            }

        
            }
        }
    }
}
